package cn.xiaopengstack.domain.strategy.service.rule.tree;

import cn.xiaopengstack.domain.strategy.model.valobj.RuleTreeNodeLineVO;
import cn.xiaopengstack.domain.strategy.service.rule.tree.impl.DefaultTreeFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author jiangyangang
 */
public interface ILogicTreeNode {

    /**
     * 执行树节点逻辑
     * @param treeMatter
     * @return
     */
    DefaultTreeFactory.TreeActionEntity logic(DefaultTreeFactory.TreeActionMatterEntity treeMatter);


    /**
     * 获取下一个节点
     * @param matterValue
     * @param treeNodeLineVOList
     * @return
     */
    default String getNextNode(String matterValue, List<RuleTreeNodeLineVO> treeNodeLineVOList) {
        if (StringUtils.isEmpty(matterValue) || CollectionUtils.isEmpty(treeNodeLineVOList)) {
            return StringUtils.EMPTY;
        }
        // 根据引用循环查找下一个可执行的节点
        for (RuleTreeNodeLineVO nodeLine : treeNodeLineVOList) {
            boolean isDecisionNode = false;
            switch (nodeLine.getRuleLimitType()) {
                case EQUAL:
                    isDecisionNode = matterValue.equals(nodeLine.getRuleLimitValue().getCode());
                // 以下规则暂时不需要实现
                    break;
                case GT:
                    break;
                case LT:
                    break;
                case GE:
                    break;
                case LE:
                    break;
                default:
                    return StringUtils.EMPTY;
            }

            if (isDecisionNode) {
                // 返回下一个节点
                return nodeLine.getRuleNodeTo();
            }
        }
        return StringUtils.EMPTY;
    }
}
